package com.example.docker.service;

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class DeepSeekService {

    private static final String API_URL = "https://api.deepseek.com/v1/chat/completions";
    private static final String API_KEY = "sk-e05a60b1444f4522aa20bc07611294ba"; // 替换为你的 API Key

    public String sendMessage(String message) {
        RestTemplate restTemplate = new RestTemplate();
        String requestBody = "{\n" +
                "        \"model\": \"deepseek-chat\",\n" +
                "        \"messages\": [\n" +
                "          {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n" +
                "          {\"role\": \"user\", \"content\": \"Hello!\"}\n" +
                "        ],\n" +
                "        \"stream\": false\n" +
                "      }'";

        try {
            String response = restTemplate.postForEntity(API_URL, requestBody, String.class,
                    "Authorization", "Bearer " + API_KEY).getBody();

            return parseResponse(response);
        } catch (Exception e) {
            e.printStackTrace();
            return "Sorry, I encountered an error. Please try again.";
        }
    }

    private String parseResponse(String response) {
        int start = response.indexOf("content\": \"") + 10;
        int end = response.indexOf("\"", start);
        return response.substring(start, end);
    }
}
